package run.halo.app.service.impl;

import org.springframework.stereotype.Service;
import run.halo.app.model.entity.Erfav;
import run.halo.app.model.entity.Erlink;
import run.halo.app.model.entity.Post;
import run.halo.app.repository.ErlinkRepository;
import run.halo.app.service.ErlinkService;
import run.halo.app.service.PostService;
import run.halo.app.service.UserService;
import run.halo.app.service.base.AbstractCrudService;

@Service
public class ErlinkServiceImpl extends AbstractCrudService<Erlink, Integer> implements ErlinkService {
    private final ErlinkRepository erlinkRepository;
    private final UserService userService;
    private final PostService postService;

    public ErlinkServiceImpl(ErlinkRepository erlinkRepository, UserService userService,PostService postService) {
        super(erlinkRepository);
        this.erlinkRepository=erlinkRepository;
        this.userService=userService;
        this.postService=postService;

    }

    @Override
    public void linkPost(Integer postId, String token) {
        Integer uid=userService.getUidFromToken(token);
        if (uid>0) {
            Erlink erlink = new Erlink();
            erlink.setUid(uid);
            erlink.setPostid(postId);
            Post post=postService.getById(postId);
            erlink.setFid(post.getUid());
            erlink.setLink(1);
            super.create(erlink);
        }
    }
}
